package com.hsogoo.study.autoconfiguration.conditional;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotatedTypeMetadata;

/**
 * @Author huangsaigang
 * Created @2019-10-3.
 */
public class OnConditionalSystemProperty implements Condition {

    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {

        AnnotationAttributes attributes = AnnotationAttributes.fromMap(annotatedTypeMetadata.getAnnotationAttributes(ConditionalOnSystemProperty.class.getName()));
        String propertyName = String.valueOf(attributes.getString("name"));
        String propertyValue = String.valueOf(attributes.getString("value"));

        String systemValue = System.getProperty(propertyName);

        if(propertyValue.equals(systemValue)){
            return true;
        }
        return false;
    }

}
